/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.serverpackets;


import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.templates.QuestTemplate;
import com.aionengine.gameserver.model.templates.quest.QuestExtraCategory;
import com.aionengine.gameserver.network.aion.AionConnection;
import com.aionengine.gameserver.network.aion.AionServerPacket;
import com.aionengine.gameserver.questEngine.model.QuestStatus;

/**
 * @author VladimirZ
 */
public class SM_QUEST_ACTION extends AionServerPacket {

    protected int questId;
    private int status;
    private int step;
    protected int action;
    private int timer;
    private int sharerId;
    @SuppressWarnings("unused")
    private boolean unk;

    SM_QUEST_ACTION() {

    }

    /**
     * Accept Quest(1)
     *
     * @param questId
     * @param status
     * @param step
     */
    public SM_QUEST_ACTION(int questId, int status, int step) {
        this.action = 1;
        this.questId = questId;
        this.status = status;
        this.step = step;
    }

    /**
     * Quest Steps/Finish (2)
     *
     * @param questId
     * @param status
     * @param step
     */
    public SM_QUEST_ACTION(int questId, QuestStatus status, int step) {
        this.action = 2;
        this.questId = questId;
        this.status = status.value();
        this.step = step;
    }

    /**
     * Delete Quest(3)
     *
     * @param questId
     */
    public SM_QUEST_ACTION(int questId) {
        this.action = 3;
        this.questId = questId;
    }

    /**
     * Display Timer(4)
     *
     * @param questId
     * @param timer
     */
    public SM_QUEST_ACTION(int questId, int timer) {
        this.action = 4;
        this.questId = questId;
        this.timer = timer;
        this.step = 0;
    }

    public SM_QUEST_ACTION(int questId, int sharerId, boolean unk) {
        this.action = 5;
        this.questId = questId;
        this.sharerId = sharerId;
        this.unk = unk;
    }

    /**
     * Display Timer(4)
     *
     * @param questId
     * @param timer
     */
    public SM_QUEST_ACTION(int questId, boolean fake) {
        this.action = 6;
        this.questId = questId;
        this.timer = 0;
        this.step = 0;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.aionemu.commons.network.mmocore.SendablePacket#writeImpl(com.aionemu.commons.network.mmocore.MMOConnection)
     */
    @Override
    protected void writeImpl(AionConnection con) {

        QuestTemplate questTemplate = DataManager.QUEST_DATA.getQuestById(questId);
        if (questTemplate != null && questTemplate.getExtraCategory() != QuestExtraCategory.NONE)
            return;
        writeC(action);
        writeD(questId);
        switch (action) {
            case 1:
                writeC(status);// quest status goes by ENUM value
                writeC(0x0);
                writeD(step);// current quest step
                writeH(0);
                writeC(0);
                break;
            case 2:
                writeC(status);// quest status goes by ENUM value
                writeC(0x0);
                writeD(step);// current quest step
                writeH(0);
                break;
            case 3:
                writeD(0);
                break;
            case 4:
                writeD(timer);// sets client timer ie 84030000 is 900 seconds/15 mins
                writeC(timer > 0 ? 1 : 0);
            case 5:
                writeD(this.sharerId);
                writeD(0);
                break;
            case 6:
                writeH(0x01);// ???
                writeH(0x0);
        }
    }
}
